<?php
/*
Options taken from berp
type
customer
customer order no.
required date
delivery address
  
 3 ECT Get.cash.type             
 5 ID Get.deposit                
13 EOT Get.order.type            
20 IP Import.pack                
21 IK Import.kit                 
28 II Import.credit.note           
31 FD Full.delivery                   
33 RR Recalculate.to.retail             
34 IQ Import.quote                      

Types. Almost certainly dealt with in database, faster, easier.
 TC SI DI
 CN PL QO
 CO DO
 Functions: Add stock. Create specials. Reserve/Unreserve stock.
            Make backorder.  Reduce stock. Create purchase order.
            Create debit (for deposits). Add to sledger update que

Note on line structure. Line price is always equal to value for delivery
of full ordered units, so a calculation is always needed to work out the
chargeable price. LINEPRICE/UNITS*DELIVERED.
*/
class sorder extends head2d {

 public $doctypes = array('QO'=>'Quote','SI'=>'Sales Invoice','SO'=>'Sales Order','CO'=>'Collection Note','CN'=>'Credit Note');
 public $converts = array('QO'=>array('QO'=>'Quote','SO'=>'Sales Order','SI'=>'Sales Invoice'),
                          'SI'=>array('SI'=>'Sales Invoice'),
                          'SO'=>array('SI'=>'Sales Invoice','SO'=>'Sales Order'),
                          'CO'=>array('CO'=>'Collection note','CN'=>'Credit Note'),
                          'CN'=>array('CN'=>'Credit Note'));
 public $showtype = 1;
 
 function sorder() {
  $this->table = "sorder";
  $this->linetable = "sorderline";
  $this->getmetadata();
  $this->maintainhist = false;
  $this->header[2] = "";
  $_SESSION[strtoupper($this->table)] = $this;
  $_SESSION["sorder"] =$this;
 }
 
 function setdisabled() {
  if ($this->header[21] == "SI" || $this->header[21] == 'CN' || $this->header[22] == 'N') {
   $this->disabled = 'disabled';
  } else {
   $this->disabled = '';
  }
 } 
 
 function getdoctype() {
  return $this->doctypes[$this->header[21]];
 }

 function getgstno() {
  $gstno = freaditem("company",'1');
  return $gstno[16];
 }
 
 function getcoreg() {
  $cono = freaditem("company",'1');
  return $cono[15];
 }
 
 function applyrefs() {
  parent::applyrefs();
  if ($this->header[5] !== "") {
    $desc = freaditem("customer",$this->header[5]);
    $this->refs[5] = $desc[1];
  }
 }
 
 function emailpdf() {
  $pdf = new showpdf();
  $pdfcode = $pdf->outfile($this);
  $this->emailatt($pdfcode);
 }
 
 function createblank() {
  $this->header = array_fill(0,$this->atts,"");
  $this->header[2] = "0";
  $this->valids = array_fill(0,$this->atts,"");
  $this->lists = array_fill(0,$this->atts,"");
  $this->refs = array_fill(0,$this->atts,"");

  $this->currentline = $this->defaultline();
  $this->currentlinerefs = array_fill(0,$this->lineatts,"");
  $this->currentlinevalids = array_fill(0,$this->lineatts,"");
  $this->currentlinelists = null;
  
  $this->lines = null;
  $this->linerefs = null;
  
  $this->header[18] = dtoiso("");
  $user = authenticate(0);
  $this->header[16] = $user[0]['branch'];
  $branchitem = freaditem("branch",$this->header[16]);
  $this->header[6] = $branchitem[5];
  $this->header[7] = $branchitem[6];
  $this->header[8] = $branchitem[7];
  $this->header[9] = $branchitem[8];
  $this->header[10] = $branchitem[9];
  $list = $this->doctypes;
     $h = "<select name=f21>";
     foreach ($list as $id=>$desc) {
      $h .= "<option value='$id'>$id - $desc</option>";
     }
     $h .= "</select>";
     $this->lists[21] = $h;
  $this->lists[21] = $h;
  $this->header[21] = "QO";
  
  $this->refs[21] = $this->getdoctype();
 }
 
 function calcdisc($line,$productitem) {
    $stmt = "select max(percent) from discount ";
    $stmt .= "where product = '".$productitem[0]."' and type = 'PR' and customer = '".$this->header[5]."' ";
    $stmt .= "or product = '".$productitem[6]."' and type = 'PG' and customer = '".$this->header[5]."' ";
    $perc = sqlread($stmt);
    $perc = $perc[0]*1;
    $line[9] = $perc;
    $net = (100-$perc)/100*$productitem[12]*($line[6]);   
    $line[4] = $net;
    return $line;
 }

 function applyfromrequest() {
  // Mapped requests F1-> field 1 etc, L1->Line 1 etc
  
  $this->valid = true;
  $this->valids = array_fill(0,$this->atts,"");
  $this->currentlinevalids = array_fill(0,$this->lineatts,"");
  
  if ($f=@$_REQUEST["f5"]) {
   if ($f !== $this->header[5]) {
    // Code of Customer object
    $desc = freaditem("customer",$f);
    if ($desc == "") {
     $this->valids[5] = "Need valid customer code";
     $this->lists[5] = $this->selectlisth("CUSTOMER",$f,"f5");
    } else {
     $this->header[5] = $f;
     $this->refs[5] = $desc[1];
     $this->lists[5] = "";
     for ($i=0;$i<sizeof($this->lines);$i++) {
      if ($this->lines[$i][5]) {
       $productitem = freaditem("product",$this->lines[$i][5]);
       $this->lines[$i] = $this->calcdisc($this->lines[$i],$productitem);
      }
     }
    }
   }
  }
    
  if ($f=@$_REQUEST["f21"]) {
   if ($f !== $this->header[21]) {
    $desc = @$this->doctypes[$f] ;
    
    if ($this->header[2] > 0) {
     $list = $this->converts[$this->origheader[21]];
	  } else {
	   $list = $this->doctypes;
 	  }
 	  
    if ($desc == "") {
     // Make up select list of possible types
     $this->valids[21] = "Need valid order type"; 
     $h = "<select name=f21>";
     foreach ($list as $id=>$desc) {
      $h .= "<option value='$id'>$id - $desc</option>";
     }
     $h .= "</select>";
     $this->lists[21] = $h;
    } else {
     // Validate new type
     if ($this->header[2] > 0) {
      // If it is not new - only certain conversions allowed
      if (isset($list[$f])) {
       $this->header[21] = $f;     
       $this->refs[21] = $desc;
       $this->lists[21] = "";
      } else {
       $this->valids[21] = "You can't convert a ".$this->origheader[21]." into a $f";
      }
     } else {
      // If it is new - just convert it
      $this->header[21] = $f;     
      $this->refs[21] = $desc;
      $this->lists[21] = "";
     }
    }
   } else {
    // No change to type
    $this->lists[21] = "";
   }
  }
  
  if( $f=@$_REQUEST["f1"]) {
   $size = $this->metadata[1]->length ;
   $a = preg_replace("/[^A-Z\na-z0-9!@#$%&*\/()+?\-\']/"," ",$f);
   $a = substr($a,0,$size);
   $this->header[1] = $a;
  }
  
  if( $f=@$_REQUEST["f19"]) {
   $size = $this->metadata[19]->length ;
   $a = preg_replace("/[^A-Z\na-z0-9!@#$%&*()+?\-\']/"," ",$f);
   $a = substr($a,0,$size);
   $this->header[19] = $a;
  }

  if( $f=@$_REQUEST["l8"]) {
   $size = $this->linemetadata[8]->length ;
   $a = preg_replace("/[^\r\nA-Za-z0-9!@#$%&*()+?\-\']/"," ",$f);
   $a = substr($a,0,$size);
   $this->currentline[8] = $a;
  }
  
  if( $f=@$_REQUEST["f6"]) {
   $size = $this->metadata[6]->length ;
   $a = preg_replace("/[^A-Z\na-z0-9!@#$%&*\/()+?\-\']/"," ",$f);
   $a = substr($a,0,$size);
   $this->header[6] = $a;
  }
  
  if( $f=@$_REQUEST["f7"]) {
   $size = $this->metadata[7]->length ;
   $a = preg_replace("/[^A-Z\na-z0-9!@#$%&*\/()+?\-\']/"," ",$f);
   $a = substr($a,0,$size);
   $this->header[7] = $a;
  }
  
  if( $f=@$_REQUEST["f8"]) {
   $size = $this->metadata[8]->length ;
   $a = preg_replace("/[^A-Z\na-z0-9!@#$%&*\/()+?\-\']/"," ",$f);
   $a = substr($a,0,$size);
   $this->header[8] = $a;
  }
  
  if( $f=@$_REQUEST["f9"]) {
   $size = $this->metadata[9]->length ;
   $a = preg_replace("/[^A-Z\na-z0-9!@#$%&*\/()+?\-\']/"," ",$f);
   $a = substr($a,0,$size);
   $this->header[9] = $a;
  }
  
  if( $f=@$_REQUEST["f10"]) {
   $size = $this->metadata[10]->length ;
   $a = preg_replace("/[^A-Z\na-z0-9!@#$%&*\/()+?\-\']/"," ",$f);
   $a = substr($a,0,$size);
   $this->header[10] = $a;
  }
  
  if( $f=@$_REQUEST["l2"]) {
   $size = $this->linemetadata[2]->length ;
   $a = substr(preg_replace("/[^A-Za-z0-9!@#$%&*()+?\-\']/"," ",$f),0,$size);
   $this->currentline[2] = $a;
  }

  $ordqty = @$_REQUEST["l3"] ;
  $delqty = @$_REQUEST["l6"] ;
  $tofollowqty = @$_REQUEST["l7"] ;
  $olddelqty = $this->currentline[6] ;
  $oldtofollowqty = $this->currentline[7];
  
  if ($ordqty && $ordqty !== $this->currentline[3]) {
   $size = $this->linemetadata[3]->length ;
   if(preg_match("/^[0-9]{0,$size}$/",$ordqty)) {
    $this->currentline[3] = $ordqty;
    $this->currentline[6] = $ordqty;
    $this->currentline[7] = "0";
   } else {
    $this->currentlinevalids[3] = "Please enter a number";
   }
  }  
  if ($delqty && $delqty !== $olddelqty) {
   $size = $this->linemetadata[6]->length ;
   if(preg_match("/^[0-9]{0,$size}$/",$delqty)) {
    $this->currentline[6] = $delqty;
    $this->currentline[7] = max(0,($this->currentline[3]-$this->currentline[6]));
   } else {
    $this->currentlinevalids[6] = "Please enter quantity";
   }
  }
  if ($tofollowqty && $tofollowqty != $oldtofollowqty) {
   $size = $this->linemetadata[7]->length ;
   if(preg_match("/^[0-9]{0,$size}$/",$tofollowqty)) {
    $this->currentline[7] = $tofollowqty;
   } else {
    $this->currentlinevalids[7] = "Please enter quantity";   
   }
  }
 
  if($f=@$_REQUEST["l5"]) {
   
    // product code
    $productitem = freaditem("product",$f);
    if ($productitem == "") {
     $this->currentlinevalids[5] = "Need valid product code";
     $this->currentlinelists[5] = $this->selectlisth("product",$f,"l5");
    } else {
     $this->currentline[5] = $f;
     $this->currentlinerefs[5] = $productitem[1];
     $this->currentlinelists[5] = "";
     $this->currentline[2] = $productitem[1];
     // Price and discount
     $this->currentline = $this->calcdisc($this->currentline,$productitem);
    }
  }

  if($f=@$_REQUEST["l4"]) {
   // line price
   $size = $this->linemetadata[4]->length ;
   $dec  = $this->linemetadata[4]->decimals;
   $len  = $size-$dec;
   if(preg_match("/^-{0,1}[0-9]{0,$len}[.]{0,1}[0-9]{0,$dec}$/",$f)) {
    $this->currentline[4] = $f;
   } else {
    $this->currentlinevalids[4] = "Please enter an amount in currency";
   }
  } 

  if($f=@$_REQUEST["uprice"]) {
    $size = $this->linemetadata[4]->length ;
    $dec  = $this->linemetadata[4]->decimals;
    $len  = $size-$dec;
    if(preg_match("/^-{0,1}[0-9]{0,$len}[.]{0,1}[0-9]{0,5}$/",$f)) {
     $res = intval($f*($this->currentline[3])*100)/100 ;
     if ($f != "") {
      $this->currentline[4] = $res;
     }
    }
  }
  
  if( $f=@$_REQUEST["f17"]) {
   // Date. 
   $a = dtoiso($f);
   if ($a) {
    $diff = isotojd()-isotojd($a) ;
    if ($diff > 30 || $diff < -90) {
     $this->valids[17] = "Date must be within three months of today";
    } else {
     $this->header[17] = $a;
    }
   } else {
    $this->valids[17] = "Invalid date";
   }
  } 

  if ($this->header[5] == "") {
   $this->valids[5] = "Cannot leave customer blank";
  }
  if ($this->header[17] == "") {
   $this->valids[17] = "Cannot leave date blank";
  }
  $this->valid = every($this->valids,""); 

  if ($this->currentline[2] == "") {
   $this->currentlinevalids[2] = "Cannot leave blank";
  }
  if ($this->currentline[4] === "") {
   $this->currentlinevalids[4] = "Please enter unit price or line price";
  }
  
   if ($apply = @$_REQUEST["sub"]) {
    if ($apply == "Complete Line" || $apply == "Apply") {
     if ($this->currentline !== $this->defaultline()) {
      if (every($this->currentlinevalids,"") ) {
       $this->lines[$this->currentline[1]] = $this->currentline;
       $this->currentline = $this->defaultline();
       $this->showtype =1 ;
      } else {
       $this->valid = false;
      }
     }
    } elseif ($apply == "Abandon Changes") {
     $this->showtype = 1;
     $this->currentline = $this->defaultline();
    }
   }

   // Unset if requested. Note that unset is slow
   (isset($_REQUEST["x"])) ? $x=$_REQUEST["x"] : $x = -1;
   if (isset($this->lines[$x])) {
    unset($this->lines[$x]);
    $this->lines = array_values($this->lines); // Removes blanks
    $this->currentline = $this->defaultline();
   }
 
	 if (isset($_REQUEST["editline"]) && (every($this->currentlinevalids,"") || $this->currentline==$this->defaultline())) {
	  $e = $_REQUEST["editline"];
	  if (isset($this->lines[$e])) {
	   $this->currentline = $this->lines[$e];
	   $this->currentline[1] = $e;
     $this->currentlinevalids = array_fill(0,$this->lineatts,"");
	  }
	 }
 }
 
 function defaultline() {
  $ans = array_fill(0,$this->lineatts,"");
  $ans[3] = "1";
  $ans[6] = "1";
  $ans[7] = "0";
  $ans[4] = "";
  $ans[1] = sizeof($this->lines);
  $ans[2] = "";
  $ans[5] = "";
  $ans[9] = "0";
  $ans[12] = "1";
  $ans[13] = "0";
  return $ans;
 }
 
 function newcounter() {
  $ans =  getcounter("QO",1);
  return $ans;
 }
 
function makehtml() {
  $co = freaditem("branch",$this->header[16]);
  $cu = freaditem("customer",$this->header[5]);
  $o = "<html>
<head>
<style>
@media print {
td.c1 {font-family:Helvetica;font-size:50px;font-weight:bolder;color:#c0c0c0;}
td.c2 {font-family:Helvetica;font-size:13px;}
td.c3 {font-family:Helvetica;font-size:12px;}
td.c4 {font-family:Helvetica;font-size:12px;border-bottom:solid 1px gray; border-left:solid 1px gray; }
td.c5 {font-family:Helvetica;font-size:12px;border-bottom:solid 1px gray; border-right:solid 1px gray; border-left:solid 1px gray}
th.h1 {font-family:Helvetica;font-size:12px;border-bottom:solid 1px gray; border-left:solid 1px gray; border-top:solid 1px gray}
th.h2 {font-family:Helvetica;font-size:12px;border-bottom:solid 1px gray; border-left:solid 1px gray; border-right:solid 1px gray; border-top:solid 1px gray}
table {margin-left:50px;margin-top:10px;margin-right:10px;}
}
@media screen {
td {display: none;}
th {display: none;}
table {display: none;}
}
</style>

</head>
<body>
<table width=90%>
<tr><td width=15%>
<img src='coldhot2.jpg' width=100px>
</td>";
  $o .= "<td class=c2 valign=top>";
  $o .= "<b>$co[1]</b><br>";
  $o .= "$co[5]<br>$co[6]<br>$co[7]<br>$co[8]<br>$co[9]<br>";
  $o .= "Tel : $co[11]<br> Email : $co[13]";
  $o .= "</td>";
  $o .= "<td valign=top align=right class=c1>Invoice</td>
</tr></table>

<table width=100%>
 <tr><td width=50%></td><td valign=top align=right class=c3>Invoice Number</td>  <td valign=top  class=c3>";
  $o .= $this->header[0];
  $o .= "</td></tr><tr>
  <td width=50%></td>
  <td valign=top align=right class=c3>Tax Date</td>
  <td valign=top class=c3>";
  $o .= $this->header[17];
  $o .= "</td></tr><tr>
  <td width=50%></td>
  <td valign=top align=right class=c3>Order Number</td>
  <td valign=top class=c3>";
  $o .= $this->header[1];
  $o .= "</td></tr></table><br><br><table width=60%><tr><td class=c3 width=30%>";
  $o .= "$cu[1]<br>$cu[5]<br>$cu[6]<br>";
  $o .= "$cu[7]<br>$cu[8]<br>$cu[9]";
  $o .= "</td><td class=c3 width=30%>";
  $o .= $this->header[1]."<br>";
  $o .= $this->header[5]."<br>";
  $o .= $this->header[6]."<br>";
  $o .= $this->header[7]."<br>";
  $o .= $this->header[8]."<br>";
  $o .= $this->header[9];
  $o .= "</td></tr></table>";
  $o .= "<br><br>";
  $o .= "<table width=100% style='border: 0pt none black;' cellspacing=0; >";
  $o .= "<tr>
 <th valign=top class=h1 align=left width=15%>Code</th>
 <th valign=top class=h1 align=left width=60%>Description</th>
 <th valign=top class=h1 align=right width=15%>Qty</th>
 <th valign=top class=h2 align=right width=10%>Price</th>
</tr>";
 $subtotal = 0;
 $ll = "";
 foreach ($this->lines as $l) {
  $ll = "";
  $ll .= "<tr>";
  $ll .= "<td valign=top class=c4>$l[5]</td>";
  $ll .= "<td valign=top class=c4>$l[2]</td>";
  $ll .= "<td align=right valign=top class=c4>$l[3]</td>";
  $ll .= "<td align=right valign=top class=c4>$l[4]</td>";
  $ll .= "</tr>";
  $o .= $ll;
  $subtotal += $l[4];
 }
 list($goods,$gst) = $this->calcgst();
 $o .= "<tr><td></td><td></td>";
 $o .= "<td align=right valign=top class=c4>Total</td>";
 $o .= "<td valign=top class=c5 align=right>".sprintf("%2.2f",$subtotal)."</td>";
 $o .= "</tr>";

 $o .= "<tr><td></td><td></td>";
 $o .= "<td align=right valign=top class=c4>GST</td>";
 $o .= "<td valign=top class=c5 align=right>".sprintf("%2.2f",$gst)."</td>";
 $o .= "</tr>";

 $o .= "<tr><td></td><td></td>";
 $o .= "<td align=right valign=top class=c4>Grand Total</td>";
 $o .= "<td valign=top class=c5 align=right>".sprintf("%2.2f",$gst+$subtotal)."</td>";
 $o .= "</tr>";
 $o .= "</table></body></html>";
 return $o;
 }
 
 function writedb() {
  if ($this->disabled=="") {
   parent::writedb();
  }
 }
 
 function showaddress() {
  echo "<br>";
  echo "<table border=0 cellpadding=0 frame=void><tr>";
  echo "<td><a accesskey='h' onclick='document.form.showtype.value=1;document.form.submit()'>Show lines</a></td>";
  echo "<td><a accesskey='n' onclick='document.form.showtype.value=3;document.form.submit();'>Note</a></td>";
  echo "</tr></table>";
  echo "<body onload='document.form.f6.focus()'>";
  echo "<table>";
  echo "<h3>Delivery address</h3>";
  echo $this->htmlrow(6,"Address Line 1");
  echo $this->htmlrow(7,"Address Line 2");
  echo $this->htmlrow(8,"Address Line 3");
  echo $this->htmlrow(9,"Address Line 4");
  echo $this->htmlrow(10,"Post Code");
  echo "</table>";
  if ($this->disabled == "") {
   echo "<input class=bt accesskey=A type=submit name=sub value=Apply>";
   echo "<input class=bt accesskey=S type=submit name=sub value=Save>";
  }
  echo "<input class=bt accesskey=P type=submit name=sub value=Print>";
  echo "<input class=bt accesskey=E type=submit name=sub value=Email>";
  echo "<button class=bt onclick=rest()><span>Restart</span></button>"; 
 }
 
 function showlines() {
  echo "<script>";
    // Ajax calls
  echo "function getprice() {
    obj = document.getElementById('idl5');
    var req = false;
    var self = this;
    self.req = new XMLHttpRequest();
		self.req.open('POST', 'ans.php', true);
    self.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.req.onreadystatechange = function() {
     if (self.req.readyState == 4) {
      document.getElementById('iduprice').value = self.req.responseText;
  	 }
    }
    self.req.send('a=' + obj.value);
  }";
  echo "</script>";
 
  echo "<br>";
  echo "<table border=0 cellpadding=0 frame=void><tr>";
  echo "<td><a accesskey='l' onclick='document.form.showtype.value=2;document.form.submit();'>Delivery Address</a></td>";
  echo "<td><a accesskey='n' onclick='document.form.showtype.value=3;document.form.submit();'>Note</a></td>";
  echo "</tr></table>";
 	echo "<br>";
	echo "<table width=60%>";
	echo "<tr><th>Actions</th><th>Product Code</th><th width=70%>Description</th><th>Qty</th><th>Unit Price</th><th>Line Price</th></tr>";
	for ($i=0;$i<=sizeof($this->lines);$i++) {
	 if ($this->currentline[1] == $i && $this->disabled != 'disabled') {
    if ($this->currentline[3] == 0 || $this->currentline[4] == "") {
     $uprice = "";
    } else {
     $uprice = $this->currentline[4]/$this->currentline[3];
    }

  	echo "<tr>\n";
   	echo "<td>Edit Line</td>";
 	  if ($this->currentlinelists[5] == "") {
     echo "<td><input onfocus='dofocus()' onblur=getprice('l5') id=idl5 type=text size=10 maxlength=16 name='l5' value='".$this->currentline[5]."'></td>\n";	
  	 if ($this->currentline[5] !== "") {$disable = "readonly=readonly";} else {$disable="";}
	 	 echo "<td><input onfocus='dofocus()' id=idl2 type=text size=45 maxlength=45 name='l2' value='".$this->currentline[2]."'></td>\n";	
    } else {
     echo "<td colspan=2>".$this->currentlinelists[5]."</td>";
    }
	  echo "<td><input id=idl3 type=text size=5 maxlength=5 name='l3' value='".$this->currentline[3]."'></td>\n";	
	  echo "<td><input id='iduprice' type=text size=5 maxlength=5 name='uprice' value='".$uprice."'></td>\n";	
	  echo "<td><input id=idl4 type=text size=5 maxlength=5 name='l4' value='".$this->currentline[4]."'></td>\n";	
	  echo "</tr>";
	} elseif (isset($this->lines[$i])) {

    if ($this->lines[$i][3] == 0) {
     $uprice = "";
    } else {
     $uprice = ($this->lines[$i][4])/($this->lines[$i][3]);
    }  
	  echo "<tr>";
		echo "<td>";
	  echo "<img title='Edit line' onclick='document.form.showtype.value=4;submitform1($i);' src='images/e.gif'>"; 	
	  if ($this->disabled != 'disabled') {
     echo "<img title='Delete line' onclick=submitform2($i) src='images/x.gif'>";
    }
	  echo "</td>";
	  echo "<td>".$this->lines[$i][5]."</td>";
	  echo "<td>".$this->lines[$i][2]."&nbsp;</td>";
	  echo "<td align=right>".$this->lines[$i][3]."&nbsp;</td>";
	  echo "<td align=right>".sprintf("%2.2f",$uprice)."</td>";
	  echo "<td align=right>".sprintf("%2.2f",$this->lines[$i][4])."</td>";
	  echo "</tr>\n";   
	 }
	}
	list($goods,$gst) = $this->calcgst();
	echo "<tr><td></td><th align=right colspan=4>Total</th><th align=right>".sprintf("%2.2f",$goods)."</th></tr>";
	echo "<tr><td></td><th align=right colspan=4>GST</th><th align=right>".sprintf("%2.2f",$gst)."</th></tr>";
	echo "<tr><td></td><th align=right colspan=4>Grand Total</th><th align=right>".sprintf("%2.2f",$gst+$goods)."</th></tr>";
	echo "</table>";
	if ($this->disabled == "") {
   echo "<input class=bt accesskey=A type=submit name=sub value=Apply>";
   echo "<input class=bt accesskey=S type=submit name=sub value=Save>";
  }
  echo "<input class=bt accesskey=P type=submit name=sub value=Print>";
  echo "<input class=bt accesskey=E type=submit name=sub value=Email>";
  echo "<button class=bt onclick=rest()><span>Restart</span></button>"; 
 }
 
 function shownote() {
 
  echo "<br>";
  echo "<table border=0 cellpadding=0 frame=void><tr>";
  echo "<td><a accesskey='h' onclick='document.form.showtype.value=1;document.form.submit()'>Show lines</a></td>";
  echo "<td><a accesskey='l' onclick='document.form.showtype.value=2;document.form.submit();'>Delivery Address</a></td>";
  echo "</tr></table>";
  echo "<body onload='document.form.f19.focus()'>";
  echo "<h3>Note</h3>";
  echo "<textarea name=f19 accesskey=T cols=40 rows=6>";
  echo $this->header[19];
  echo "</textarea>";
  echo "<br>";
  if ($this->disabled) {
   echo "<input class=bt accesskey=A type=submit name=sub value=Apply>";
   echo "<input class=bt accesskey=S type=submit name=sub value=Save>";
  }
  echo "<input class=bt accesskey=P type=submit name=sub value=Print>";
  echo "<input class=bt accesskey=E type=submit name=sub value=Email>";
  echo "<button class=bt onclick=rest()><span>Restart</span></button>"; 
 }
 
 function linenote() {
  echo "<br>";
  echo "<table border=0 cellpadding=0 frame=void><tr>";
  if ($this->currentline[1] == sizeof($this->lines)) {
   echo "<td><a accesskey='l' onclick='document.form.showtype.value=1;document.form.submit();'>Show Lines</a></td>";
  }
  echo "<td><a accesskey='o' onclick='document.form.showtype.value=4;document.form.submit();'>More Line Options</a></td>";
  echo "</tr></table>";
  echo "<body onload='document.form.l8.focus()'>"; 
  echo "<h3>Line Note for line ".(1+$this->currentline[1])."</h3>";
  echo "<textarea name=l8 accesskey=T cols=40 rows=6>";
  echo $this->currentline[8];
  echo "</textarea>";
  echo "<br>";
  if ($this->disabled == "") {
   echo "<input class=bt accesskey=A type=submit name=sub value='Apply Line'>";
   echo "<input class=bt accesskey=C type=submit name=sub value='Complete Line'>";
  }
  echo "<input class=bt accesskey=B type=submit name=sub value='Abandon Changes'>";
 }
 
 function lineoptions() {
  echo "<br>";
  echo "<table border=0 cellpadding=0 frame=void><tr>";
  echo "<td><a accesskey='n' onclick='document.form.showtype.value=5;document.form.submit();'>Line Note</a></td>";
  if ($this->currentline[1] == sizeof($this->lines)) {
   echo "<td><a accesskey='l' onclick='document.form.showtype.value=1;document.form.submit();'>Show Lines</a></td>";
  }
  echo "</tr></table>";
  
   echo "<body onload='document.form.l3.focus()'>";
   echo "<h3>Editing Line ".($this->currentline[1]+1)."<h3>";
   echo "<table>";
   echo $this->htmlline("2","Description");
   echo $this->htmlline("4","Line Price");
   echo $this->htmlline("3","Order Qty");
   echo $this->htmlline("6","Qty Delivered");
   echo $this->htmlline("7","Qty To Follow");
   echo "</table>";
   if ($this->disabled == "") {
    echo "<input class=bt accesskey=A type=submit name=sub value='Apply Line'>";
    echo "<input class=bt accesskey=C type=submit name=sub value='Complete Line'>";
   }
   echo "<input class=bt accesskey=B type=submit name=sub value='Abandon Changes'>";  
 }
 
 function showform() {
  $h = $this->header;
  $m = $this->metadata;
  $e = $this->valids;
  $c = $this->currentline;
  $le = $this->currentlinevalids;
  $focalpoint = "f5";
  if ($h[5] == "" || $e[5] != "") {$focalpoint = "f5";}
  elseif ($h[1] == "" || $e[1] != "") {$focalpoint = "f1";}
  elseif ($h[17] == "" || $e[17] != "") {$focalpoint = "f17";}
  elseif ($c[5] == "") {$focalpoint = "l5";}
  echo "<head>";
  echo "<script src='datechooser.js' type='text/javascript'></script>";
  echo SCRIPTS ;
  // User enters either a product code or description. Script to switch.
  echo "<script>";
  echo "function dofocus() {";
  echo "if (document.form.l5.value == '' && document.form.l2.value == '') {";
  echo " document.getElementById('idl5').readOnly=false;";
  echo " document.getElementById('idl2').readOnly=false;";
  echo "} else if (document.form.l5.value == '' && document.form.l2.value != '') {";
  echo " document.getElementById('idl5').readOnly=true;";
  echo " document.getElementById('idl2').readOnly=false;";
  echo "} else if (document.form.l5.value != '') {";
  echo " document.getElementById('idl2').readOnly=true;";
  echo " document.getElementById('idl5').readOnly=false;";
  echo "}";
  
  echo "}";
  echo "</script>";
  //
  echo "</head>";
	echo "<body onload='document.form.".$focalpoint.".focus()'>";
  echo "<h1>".$this->doctypes[$this->header[21]]." Entry</h1>"; 
  echo "<form name=form method=POST>";
  echo "<input type=hidden name=x value=-1>\n";
  echo "<input type=hidden name=editline value=-1>";
  echo "<input type=hidden name=res value=''>";
  echo "<input type=hidden name=showtype value=-1>";
  echo "<table>";
  
  // $h[2]. Blank is uninitiated. Zero is new. Anything else is editing.
  echo $this->htmlrow("21","Document Type"); 
  if ($h[2] !== "0") {
   echo "<tr>";
   echo " <td>Order Number</td>";
   echo " <td>$h[0]</td>";
   echo "</tr>";
  }
  echo $this->htmlrow(5,"Customer");
  echo $this->htmlrow(1,"Order Number");

  // Includes callout date selector
  echo "<tr><td>Delivery Date</td><td>";
  $dt = isotoout($this->header[17]);
  echo "<input $this->disabled id=dot name=f17 size=10 maxlength=10 type=text value='$dt'>\n"; 
  if ($this->disabled != "disabled") {
   $dc = "showChooser(this, 'dot', 'chooserSpan', 1950, 2010, 'd-m-Y', false);";
   echo "<img src='calendar.gif' onclick=".'"'.$dc.'"'.">\n";
   echo "<div id='chooserSpan' class='dateChooser' style='display: none; visibility: hidden; width: 160px;'>\n";
   echo "</div>\n";
  }
  echo "</td>";
  echo "<td>".$this->valids[17]."</td>";  
  echo "</tr>";

  echo "</table>";
  
  isset($_REQUEST["showtype"]) ? $newshow = $_REQUEST["showtype"] : $newshow = -1;
  
  // Cannot move on while there are errors
  if ($this->valid) {
   if ($newshow > 0 && $newshow < 10) {
    if ($this->currentline[1] == sizeof($this->lines)) {
     if ($newshow == 1 || $newshow == 2 || $newshow == 3) {
       $this->showtype=$newshow;
     }
    } else {
     $this->showtype=$newshow;
    }
   }
	}
	
  if ($this->showtype==2) {$this->showaddress();
  } elseif ($this->showtype==3) {$this->shownote();
  } elseif ($this->showtype==4) {$this->lineoptions();
  } elseif ($this->showtype==5) {$this->linenote();
	} else {$this->showlines();
  }
  
  echo "</form>"; 
  // set up help
  echo($this->registerhelp());
 }

}
?>